/**
 * 
 */
package it.irondev.appturismo.action;

import it.irondev.appturismo.bean.MapEntity;
import it.irondev.appturismo.entity.Attractions;
import it.irondev.appturismo.entity.Event;
import it.irondev.appturismo.entity.Restaurant;
import it.irondev.appturismo.filter.BaseFilter;
import it.irondev.appturismo.filter.RestaurantFilter;
import it.irondev.appturismo.service.IAttractionService;
import it.irondev.appturismo.service.IEventService;
import it.irondev.appturismo.service.ILanguageService;
import it.irondev.appturismo.service.IRestaurantService;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author federicocerruto
 *
 */
@Controller("mobileAction")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class MobileAction extends ActionSupport  {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -9031986678221250761L;

	@Autowired
	IRestaurantService serviceRestaurant;	
	
	@Autowired
	IAttractionService serviceAttraction;
	
	@Autowired
	IEventService serviceEvent;
	
	@Autowired
	ILanguageService serviceLanguage;
	
	
	
	private List<MapEntity> mapEntity;
	
	private List<Event> events;
	
	public String getMap() {
		return "aroundMe";
	}
	
	
	public String home() {
		BaseFilter filter = new BaseFilter();
		filter.setIdlanguage(serviceLanguage.getLanguageByPrefix(ActionContext.getContext().getLocale().getLanguage()).getIdlanguage());
		events = serviceEvent.getEventsByFilter(filter);		
		return "homeMobile";
	}
	
	
	public String getAllEntityJson() {
		BaseFilter filter = new BaseFilter();
		RestaurantFilter filterR = new RestaurantFilter();
		Integer idLanguage = serviceLanguage.getLanguageByPrefix(ActionContext.getContext().getLocale().getLanguage()).getIdlanguage();
		filter.setIdlanguage(idLanguage);
		filterR.setIdlanguage(idLanguage);
		List<Restaurant> restaurants = serviceRestaurant.getRestaurantsByFilter(filterR);
		List<Attractions> attractions = serviceAttraction.getAttractionsByFilter(filter);
		mapEntity = new ArrayList<MapEntity>();
		for (Attractions attraction : attractions) {
			MapEntity entity = new MapEntity();
//			attraction
			entity.setEntityType(0);
			try {
				BeanUtils.copyProperties(entity, attraction);
				BeanUtils.copyProperty(entity, "entityId", attraction.getIdattraction());
				BeanUtils.copyProperty(entity, "cityName", attraction.getCity().getName());
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			mapEntity.add(entity);
		}
		for (Restaurant restaurant : restaurants) {
			MapEntity entity = new MapEntity();
//			restaurants
			entity.setEntityType(1);
			try {
				BeanUtils.copyProperties(entity, restaurant);
				BeanUtils.copyProperty(entity, "entityId", restaurant.getIdrestaurant());
				BeanUtils.copyProperty(entity,"cityName", restaurant.getCity().getName());
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			mapEntity.add(entity);
		}
		
		return "getAllEntityJsonMobile";
	}


	public List<MapEntity> getMapEntity() {
		return mapEntity;
	}


	public void setMapEntity(List<MapEntity> mapEntity) {
		this.mapEntity = mapEntity;
	}


	public List<Event> getEvents() {
		return events;
	}


	public void setEvents(List<Event> events) {
		this.events = events;
	}


	

}
